import {leaveDateApply} from '../../../api/myCourse'
Component({
  data:{
    dialogShow:false,
    leaveDate:"", //请假日期
    stuName:"", //学员姓名
    leaveCourse:"", //请假课程
    memberList:[],
    remark:"" //请假备注
  },
  
  methods:{
    onShow(){
      console.log(this.data.leaveCourseList)
   
    },
    onLoad(){
     this.getMemberList()

    },
    changeStudentStatus(e:any){
     const {value} = e.detail
     this.setData({
      remark:value
     })
    },

    // 获取学员列表
    getMemberList(){
    const memberList = getApp().getMemberList().map((val:{stuId:number,userName:string})=>{
      return {
        id:val.stuId,
        name:val.userName
      }
    })
    this.setData({
      memberList
    })
    },
    async confirmLeave(){
      const stuId = Number(this.data.stuId) 
      const reason = this.data.remark
      if(!this.data.stuId || (!this.data.leaveCourseList || this.data.leaveCourseList.length <=0 )|| reason === ""){
return wx.Utils.toast("必填项不能为空")
      }

      const lessonIds = this.data.leaveCourseList.map((val:string)=>{
       return Number(val)
      })
      const postData:{
        stuId:number,
        lessonIds:Array<number>,
        reason:string
      } = {
        stuId,
        lessonIds,
        reason
      }
    const {code,data} = await leaveDateApply(postData)
      if(code === 200 && data){
        this.setData({
          dialogShow:true
        })
      }
    },
    cancelDiolog(){
      this.setData({
        dialogShow:false
      })
      wx.navigateTo({
        url:"/leaveDate_package/pages/teacherLeaveDateRecord/teacherLeaveDateRecord"
      })
    },
    //选择日历
    selectCalendar(e:any){
      this.setData({
        leaveDate:e.detail
      })
    },
    // 选择学员
    memberHandle(e:any){
    const {value} = e.detail
   const newList:Array<{id:number,name:string}> | null =  this.data.memberList.filter((val:{name:string})=>{
     return value === val.name
    })
    if(newList.length === 1){
      this.setData({
        stuId:newList[0].id,
        stuName:newList[0].name
      })
    }
    },
    //选择请假课程
    slectLeaveCourse(){
     let leaveDate = this.data.leaveDate;
     let stuId = this.data.stuId
     if(!stuId) return wx.Utils.toast("请选择请假学员")
     if(!leaveDate) return wx.Utils.toast("请选择请假时间")
     wx.navigateTo({
       url:`/curriculum_package/pages/myCurriculum/myCurriculum?leaveDate=${leaveDate}&stuId=${stuId}&review=${this.data.leaveCourseList}`
     })
    },
    // 查看请假记录
    toLeaveRecord(){
      wx.navigateTo({
        url:"/leaveDate_package/pages/teacherLeaveDateRecord/teacherLeaveDateRecord"
      })
    }
  }
})